package com.barnett.http;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;

import com.barnett.utils.L;

/**
 * TODO<上传参数线程>
 * 
 * @author JiangJian
 * @data: 2014-6-4 上午10:31:10
 * @version: V1.0
 */
public class UploadFileThread implements Runnable {
	private static Handler handler = new Handler();
	private OnHttpLinstener onHttpLinstener;
	private String params;
	private Map<String, File> files;
	private String url;

	public UploadFileThread(String url, String params, Map<String, File> files,
			OnHttpLinstener onHttpLinstener) {
		this.onHttpLinstener = onHttpLinstener;
		this.params = params;
		this.files = files;
		this.url = url;
	}

	@Override
	public void run() {
		try {
			// Looper.prepare();
			L.e("url----->" + this.url);
			L.e("params-->" + (params != null ? params.toString() : "")
					+ (files != null ? files.toString() : ""));
			Map<String, String> paramsMap = new HashMap<String, String>();
			if (params!=null) {
				paramsMap.put("paramData", params);
			}
			final String json = HttpRequest.uploadFile(this.url, paramsMap,
					files);
			if (json == null) {
				if (onHttpLinstener != null) {
					handler.post(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							onHttpLinstener.onFail(-1, "请求失败");
						}
					});
				}
				return;
			}
			L.e("返回信息---->" + json);
			JSONObject jsonObject = new JSONObject(json);
			if (jsonObject.has("result")) {
				String resultStr = jsonObject.getString("result");
				if ("1".equals(resultStr)) {
					if (onHttpLinstener != null) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onHttpLinstener.onSuccess(0, json);
							}
						});
					}
				} else {
					if (onHttpLinstener != null) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								onHttpLinstener.onFail(-1, "请求失败");
							}
						});
					}
				}
			} else {
				if (onHttpLinstener != null) {
					handler.post(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							onHttpLinstener.onFail(-1, "请求失败");
						}
					});
				}
			}
			// Looper.loop();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			if (onHttpLinstener != null) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						onHttpLinstener.onFail(-1, "请求失败");
					}
				});
			}
		} catch (JSONException e) {
			e.printStackTrace();
			if (onHttpLinstener != null) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						onHttpLinstener.onFail(-1, "请求失败");
					}
				});
			}
		} catch (IOException e) {
			e.printStackTrace();
			if (onHttpLinstener != null) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						onHttpLinstener.onFail(-1, "请求失败");
					}
				});
			}
		}
	}
}
